$(function () {
    sessionStorage.clear();
    $(".js-login-btn").click(function () {
        if (checknickname() && checkpasswd()) {
            // 获取默认地址
            var loading = weui.loading('loading', {
                className: 'custom-classname'
            });
            var nickname = $('#usernickname').val();
            var passwd = $('#passwd').val();
            var result = Model.user.login({
                nickname: nickname,
                passwd: passwd
            });
            if (result.code == 1) {
                loading.hide();
                weui.toast('登录成功', {
                    duration: 1500,
                    className: 'custom-classname',
                    callback: function () {
                        location.href = 'index.html';
                    }
                });
            } else {
                loading.hide();
                weui.toast('账号或密码错误，请重新输入', {
                    duration: 1500,
                    className: 'custom-classname',
                    callback: function () {
                        location.reload()
                    }
                });

            }
        }

    });

    function checknickname() {
        var nickname = $('#usernickname').val();
        if (nickname == null || nickname == "") {
            // 输入不合法
            weui.alert('请输入账号');
            return false;
        }
        return true;
    }

    function checkpasswd() {
        var passwd = $('#passwd').val();
        if (passwd == null || passwd == "") {
            //提示错误
            weui.alert('请输入密码');
            return false;
        }
        return true;
    }
});